from django.db import models
from django.template import Template
from django.template.loader import render_to_string


# Create your models here.
# 这个类实现帮好友找对象，上传者要录入上传者信息和好友信息
class Candidate(models.Model):
    candidate_name = models.CharField(max_length=100, blank=False)
    candidate_gender = models.CharField(max_length=4, blank=True)
    candidate_birthday = models.DateField(blank=True, null=True)

    # 图片这里比较难搞，先去settings里面设置media url
    # 图片将会保存到./media/photos/2017/02/01/目录下，每天的图片为一个文件夹
    # candidate_photo = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True, null=True)

    candidate_school = models.CharField(max_length=200, blank=True)
    candidate_grade = models.CharField(max_length=100, blank=True)
    candidate_home = models.CharField(max_length=100, blank=True)

    candidate_phone = models.CharField(max_length=20, blank=True)
    candidate_wechat = models.CharField(max_length=20, blank=True)
    candidate_QQ = models.CharField(max_length=30, blank=True)

    candidate_describe = models.TextField(max_length=600, blank=True)
    candidate_expect = models.TextField(max_length=400, blank=True)
    uploader_appraise = models.TextField(max_length=500, blank=True)

    uploader_name = models.CharField(max_length=100, blank=False)
    uploader_wechat = models.CharField(max_length=20, blank=True)
    uploader_phone = models.CharField(max_length=20, blank=True)
    uploader_school = models.CharField(max_length=50, blank=True)
    uploader_campus_card = models.ImageField(upload_to='verify/%Y/%m/%d', blank=True, null=True)
    uploader_campus_card_url = models.URLField(blank=True, null=True)

    uploader_candidate_relation = models.CharField(max_length=20, blank=True)
    # 自动设置添加的时间，无需手动设置，提取指定日期的上传条目可以索引该值
    upload_time = models.DateField(auto_now_add=True)

    # 该标志位标记candidate是否审核通过
    is_verify = models.BooleanField(default=False, blank=True)

    def __str__(self):
        return self.candidate_name

    def candidate_photos(self):
        photos = self.candidatephoto_set.all()
        return render_to_string('candidate_photo.html', {'images': photos})

    def upload_verify_evidence(self):
        return render_to_string('upload_campus_card.html', {'image': self})


class CandidatePhoto(models.Model):
    candidate = models.ForeignKey(Candidate)
    candidate_photo_url = models.URLField(blank=True, null=True)
    candidate_photo = models.ImageField(upload_to='photos/%Y/%m/%d', blank=True, null=True)



